package com.xayy.health.mgt.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.kawins.base.result.CheckResult;
import cn.kawins.mybatis.base.BaseServiceImpl;
import com.xayy.health.core.constant.HealthManagerConstant;
import com.xayy.health.mgt.mapper.TestLindormMapper;
import com.xayy.health.mgt.model.TestLindorm;
import com.xayy.health.mgt.service.TestLindormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author dongxingli
 * createTime 2023/9/26
 * @version V1.0
 */
@Service
public class TestLindormServiceImpl extends BaseServiceImpl<TestLindorm> implements TestLindormService {
    @Autowired
    private TestLindormMapper testLindormMapper;

    /**
     * 导入sql脚本
     *
     * @param sqlPath sql文件路径
     * @return CheckResult<?>
     */
    @Override
    public CheckResult<?> importData(String sqlPath) {

        if (!FileUtil.exist(sqlPath)) {
            return CheckResult.fail("文件" + sqlPath + "不存在");
        }
        String sqlstring = FileUtil.readUtf8String(sqlPath);
        int size = 0;
        for (String sql : sqlstring.split(HealthManagerConstant.SING)) {
            if (StrUtil.equalsAny(sql, "\r\n", "\n", "")) {
                continue;
            }
            int i = testLindormMapper.executeSql(sql + ";");
            size += i;
        }
        return CheckResult.success("导入" + size + "条数据");
    }

    /**
     * 清除mysql数据
     * @return Object
     */
    @Override
    public CheckResult<?> truncateMysql() {
        List<String> list = new ArrayList<>();
        String tbTaskInfoName = "DELETE FROM tb_task_info";
        list.add(tbTaskInfoName);
        String tbTaskChildInfoName = "DELETE FROM tb_task_child_info";
        list.add(tbTaskChildInfoName);
        String tbTaskParamInfo = "DELETE FROM tb_task_param_info";
        list.add(tbTaskParamInfo);
        String tbModelTrainRecord = "DELETE FROM tb_model_train_record";
        list.add(tbModelTrainRecord);
        String tbModelTrainAccuracy = "DELETE FROM tb_model_train_accuracy";
        list.add(tbModelTrainAccuracy);
        String tbModelAlgorithm = "DELETE FROM model_algorithm_info";
        list.add(tbModelAlgorithm);
        for (String sql : list) {
            testLindormMapper.truncateMysql(sql);
        }
        return CheckResult.success("mysql数据清除成功");
    }

    /**
     * 清除Hologres数据
     * @return Object
     */
    @Override
    public Object cleanHologres() {
        List<String> list = new ArrayList<>();
        String tbCalculateName = "DELETE FROM tb_calculate_data";
        list.add(tbCalculateName);
        String tbDiagnoseFaultName = "DELETE FROM tb_diagnose_fault_data";
        list.add(tbDiagnoseFaultName);
        String tbDiagnoseMultipleParametersName = "DELETE FROM tb_diagnose_multiple_parameters_data";
        list.add(tbDiagnoseMultipleParametersName);
        String tbDiagnosePredictName = "DELETE FROM tb_diagnose_predict_data";
        list.add(tbDiagnosePredictName);
        String tbPhName = "DELETE FROM tb_ph_data";
        list.add(tbPhName);
        String tbTcName = "DELETE FROM tb_tc_data";
        list.add(tbTcName);
        String tbTyName = "DELETE FROM tb_ty_data";
        list.add(tbTyName);
        for (String sql : list) {
            testLindormMapper.cleanHologres(sql);
        }
        return CheckResult.success("Hologres数据清除成功");
    }

    /**
     * 清除mysql数据根据表名
     * @param tableName 表名
     * @return CheckResult<?>
     */
    @Override
    public CheckResult<?> truncateMysqlByTableName(String tableName) {
        String sql = "DELETE FROM " + tableName;
        testLindormMapper.truncateMysql(sql);
        return CheckResult.success(tableName + "数据清除成功");
    }
}
